<template>
  <div class="jeanPicker" :class="{jeanPickerDisabled: jeanPickerDisabled, jeanPickerAdd: addBtn, jeanPickerLong: pickerLong}" @click="openJeanPicker">
    {{jeanPickerName}}
    <i class="iconfont icon-arrow_down arrow-right"></i>
    <div :class="{'popup-slide-bottom-enter': jeanPickerLeaveBottom}" class="mint-popup mint-popup-bottom" v-show="showJeanPicker" style="width: 100%;z-index: 1000;">
      <div class="picker-toolbar">
        <span class="mint-datetime-action mint-datetime-cancel" @click="hideJeanPicker">取消</span>
        <span class="mint-datetime-action mint-datetime-confirm" @click="getJeanPickerValue">确定</span>
      </div>
      <mt-picker :slots="jeanPickerSlots" ref="jeanPicker" @change="jeanPickerChange" :valueKey="'key'"></mt-picker>
    </div>
    <div class="v-modal" style="z-index: 999;" v-show="showJeanPicker" @click="hideJeanPicker"></div>
  </div>
</template>

<script>
export default {
  name: 'jeanPicker',
  props: ['jeanPickerData', 'jeanPickerDefaultValue', 'jeanPickerDisabled', 'addBtn', 'changeShowData', 'pickerLong'],
  data () {
    return {
      showJeanPicker: false,
      jeanPickerLeaveBottom: true,
      jeanPickerValue: '',
      keyValueObj: {},
      defaultIndex: '',
      jeanPickerSlots: [],
      jeanPickerName: ''
    }
  },
  mounted () {
    this.setJeanPickerSlots()
    this.setJeanPickerName()
  },
  methods: {
    openJeanPicker () {
      if (!this.jeanPickerDisabled) {
        this.showJeanPicker = true
        setTimeout(() => {
          this.jeanPickerLeaveBottom = false
        }, 100)
      }
    },
    hideJeanPicker () {
      this.jeanPickerLeaveBottom = true
      setTimeout(() => {
        this.showJeanPicker = false
      }, 100)
    },
    jeanPickerChange (ref, data) {
      this.jeanPickerValue = data
    },
    getJeanPickerValue () {
      this.jeanPickerLeaveBottom = true
      setTimeout(() => {
        this.showJeanPicker = false
      }, 100)
      if (this.jeanPickerValue.length) {
        if (this.changeShowData) {
          this.jeanPickerName = this.changeShowData[this.jeanPickerValue[0].value]
        } else {
          this.jeanPickerName = this.jeanPickerValue[0].key
        }
        let jeanPickerValue = this.jeanPickerValue[0].value
        this.$emit('getJeanPicker', jeanPickerValue)
      }
    },
    setJeanPickerSlots () {
      if (this.jeanPickerData) {
        let defaultIndex = 0
        if (this.jeanPickerDefaultValue) {
          for (let i = 0; i < this.jeanPickerData.length; i++) {
            if (this.jeanPickerData[i].value === this.jeanPickerDefaultValue) {
              defaultIndex = i
              break
            }
          }
        }
        this.jeanPickerSlots = [{
          flex: 1,
          values: this.jeanPickerData,
          textAlign: 'center',
          defaultIndex: defaultIndex
        }]
      }
    },
    setJeanPickerName () {
      if (this.jeanPickerDefaultValue) {
        for (let i = 0; i < this.jeanPickerData.length; i++) {
          if (this.jeanPickerData[i].value === this.jeanPickerDefaultValue) {
            // this.jeanPickerName = this.jeanPickerData[i].key
            if (this.changeShowData) {
              this.jeanPickerName = this.changeShowData[this.jeanPickerData[i].value]
            } else {
              this.jeanPickerName = this.jeanPickerData[i].key
            }
          }
        }
      }
    }
  },
  watch: {
    showJeanPicker (val) {
      if (val) {
        document.querySelector('body').style.position = 'fixed'
      } else {
        document.querySelector('body').style.position = ''
      }
    },
    jeanPickerDefaultValue (val, oldVal) {
      if (val && val !== 'pOne') {
        for (let i = 0; i < this.jeanPickerData.length; i++) {
          if (this.jeanPickerData[i].value === this.jeanPickerDefaultValue) {
            // this.jeanPickerName = this.jeanPickerData[i].key
            if (this.changeShowData) {
              this.jeanPickerName = this.changeShowData[this.jeanPickerData[i].value]
            } else {
              this.jeanPickerName = this.jeanPickerData[i].key
            }
          }
        }
      } else if (val === 'pOne') {
        this.jeanPickerName = '1年交'
      }
      if (oldVal) {
        if (this.jeanPickerData) {
          let defaultIndex = 0
          if (this.jeanPickerDefaultValue) {
            for (let i = 0; i < this.jeanPickerData.length; i++) {
              if (this.jeanPickerData[i].value === this.jeanPickerDefaultValue) {
                defaultIndex = i
                break
              }
            }
          }
          this.jeanPickerSlots = [{
            flex: 1,
            values: this.jeanPickerData,
            textAlign: 'center',
            defaultIndex: defaultIndex
          }]
        }
      }
    },
    jeanPickerData (val) {
      if (val) {
        let defaultIndex = 0
        if (this.jeanPickerDefaultValue) {
          for (let i = 0; i < this.jeanPickerData.length; i++) {
            if (this.jeanPickerData[i].value === this.jeanPickerDefaultValue) {
              defaultIndex = i
              break
            }
          }
        }
        this.jeanPickerSlots = [{
          flex: 1,
          values: this.jeanPickerData,
          textAlign: 'center',
          defaultIndex: defaultIndex
        }]
      }
    }
  }
}
</script>

<style scoped>
  .jeanPicker {
    width: 140px;
    height: 25px;
    border: 1px solid #c9c9c9;
    text-align: left;
    line-height: 25px;
    position: absolute;
    right: 0;
    top: 2px;
    padding: 0 5px;
    font-size: 13px;
    background-color: #ffffff;
    border-radius: 3px;
  }
  .jeanPickerAdd {
    width: 110px;
    right: 30px;
  }
  .jeanPickerLong {
    width: 200px;
  }
  .jeanPicker.jeanPickerDisabled {
    background-color: #dbdbdb;
  }
  /*.jeanPickerDisabled .icon-arrow_down{*/
    /*color: #c9c9c9;*/
  /*}*/
  .arrow-right {
    float: right;
    font-size: 12px;
    line-height: 25px;
    color: #8a8a8a;
  }
</style>
